//server.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>

#define   MSG_FILE "msgserver.c" 
#define   BUFFER 255 
#define   PERM S_IRUSR|S_IWUSR 
/* 服务端创建的消息队列最后没有删除,我们要使用ipcrm命令来删除的 */
/* ipcrm -q <msqid> */

struct msgtype { 
    long mtype; 
    char buffer[BUFFER+1]; 
};

int main() 
{ 
    struct msgtype msg; 
    key_t key; 
    int msgid; 
    
    if((key=ftok(MSG_FILE,'a'))==-1) 
    { 
        fprintf(stderr,"Creat Key Error:%s\n", strerror(errno)); 
        exit(1); 
    }

    if((msgid=msgget(key, PERM|IPC_CREAT|IPC_EXCL))==-1) 
    {
        fprintf(stderr, "Creat Message Error:%s\n", strerror(errno)); 
        exit(1);
    } 
    printf("msqid = %d\n", msgid);
    while(1)
    { 
        //取类型为1的消息
        msgrcv(msgid, &msg, BUFFER + 1, 1, 0); 
        fprintf(stderr,"Server Receive:%s\n", msg.buffer); 
        msg.mtype = 2; 
        strcpy(msg.buffer + strlen(msg.buffer), "\nthis is sent by server.\n\0");
        msgsnd(msgid, &msg, BUFFER + 1, 0); 
    } 
    exit(0); 
}
